var createError = require('http-errors');
var express = require('express');
var path = require('path');
var cookieParser = require('cookie-parser');
var logger = require('morgan');

var studentRouter = require('./routes/student.js');
var majorRouter = require('./routes/major.js');
var booksRouter = require('./routes/books.js');
var leaseRouter = require('./routes/lease.js');
var teacherRouter = require('./routes/teacher.js');
var prominentTeacherRouter = require('./routes/prominentTeacher.js');
var houseparentRouter = require('./routes/houseparent.js');
var dormitoryRouter = require('./routes/dormitory.js');
var usersRouter = require('./routes/users.js');
var roleRouter = require('./routes/role.js');
var homeRouter = require('./routes/home.js');

var app = express();

app.all('*', (req, res, next) => {
  // google需要配置，否则报错cors error
  res.setHeader('Access-Control-Allow-Credentials', 'true')
  // 允许的地址,http://127.0.0.1:9000这样的格式
  res.setHeader('Access-Control-Allow-Origin', req.get('Origin'))
  // 允许跨域请求的方法
  res.setHeader(
    'Access-Control-Allow-Methods',
    'POST, GET, OPTIONS, DELETE, PUT'
  )
  // 允许跨域请求header携带哪些东西
  res.header(
    'Access-Control-Allow-Headers',
    'Origin, X-Requested-With, Content-Type, Accept, If-Modified-Since'
  )
  next()
})

// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'pug');

app.use(logger('dev'));
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));

app.use('/api/student', studentRouter);
app.use('/api/major', majorRouter);
app.use('/api/books', booksRouter);
app.use('/api/lease', leaseRouter);
app.use('/api/teacher', teacherRouter);
app.use('/api/prominentTeacher', prominentTeacherRouter);
app.use('/api/houseparent', houseparentRouter);
app.use('/api/dormitory', dormitoryRouter);
app.use('/api/users', usersRouter);
app.use('/api/role', roleRouter);
app.use('/api/home', homeRouter);

// catch 404 and forward to error handler
app.use(function(req, res, next) {
  next(createError(404));
});

// error handler
app.use(function(err, req, res, next) {
  // set locals, only providing error in development
  res.locals.message = err.message;
  res.locals.error = req.app.get('env') === 'development' ? err : {};

  // render the error page
  res.status(err.status || 500);
  res.render('error');
});

module.exports = app;
